package com.muyun.data.modeler.sysadmin.api.controller;

import com.muyun.data.modeler.infrastructure.general.consts.MessageConsts;
import com.muyun.data.modeler.infrastructure.general.exceptions.TipException;
import com.muyun.data.modeler.infrastructure.general.specs.ApiResponse;
import com.muyun.data.modeler.infrastructure.general.toolkit.RsaToolkit;
import com.muyun.data.modeler.sysadmin.api.request.AddUserRequest;
import com.muyun.data.modeler.sysadmin.api.request.EncryptRequest;
import com.muyun.data.modeler.sysadmin.application.service.UserAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 用户的新增，修改，删除等操作
 *
 * @author muyun12
 * @date 2020-12-19 10:31:30
 * @since 0.1.0
 */
@Api(value = "用户管理", tags = "用户管理")
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class UserAdminController {

    private String publicKey;
    private String privateKey;
    private MessageSourceAccessor messageSourceAccessor;
    private UserAdminService userAdminService;

    @ApiOperation(value = "加密给定字符串", notes = "用于生成用户名密码密文")
    @PostMapping(value = "/encrypt")
    public ApiResponse encrypt(@Validated @RequestBody EncryptRequest encryptRequest) throws Exception {
        return ApiResponse.success(RsaToolkit.encryptBase64ByPrivateKey(encryptRequest.getText(), privateKey));
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/user")
    public ApiResponse addUser(@Validated @RequestBody AddUserRequest addUserRequest){
        // 解密后的用户名和密码
        String decryptUsername;
        String decryptPassword;
        try {
            // 解密用户名和密码
            decryptUsername = RsaToolkit.decryptByPublicKeyWithAnyLength(addUserRequest.getUsername(), publicKey);
            decryptPassword = RsaToolkit.decryptByPublicKeyWithAnyLength(addUserRequest.getPassword(), publicKey);
        } catch (Exception e) {
            throw new TipException(messageSourceAccessor.getMessage(MessageConsts.CODE_1001));
        }
        // 创建一个用户
        userAdminService.createUser(decryptUsername, decryptPassword);
        return ApiResponse.success();
    }

    @Value("${data-modeler.public-key}")
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    @Value("${data-modeler.private-key}")
    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    @Autowired
    public void setMessageSourceAccessor(MessageSourceAccessor messageSourceAccessor) {
        this.messageSourceAccessor = messageSourceAccessor;
    }
    @Autowired
    public void setUserAdminService(UserAdminService userAdminService) {
        this.userAdminService = userAdminService;
    }
}
